
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Youz.Common.Helper;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// CR_BankCard Data Class
    /// </summary>
    public partial class CR_BankCardDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CR_BankCard";

        public CR_BankCardDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CR_BankCard model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [CR_BankCard](");
            strSql.Append("[IsDefault],[Type],[Password],[CreatedOn],[Name],[IDNo],[CardNo],[CVV2],[ExpiryDate],[BankCode],[BankName],[BankAbbr],[BillingDate],[RepaymentDate],[BillingEmail],[BankMobile],[MemberAccount],[RepaymentAmount],[RepaymentedAmount],[DepositAmount],[ModifiedOn]");
            strSql.Append(") values (");
            strSql.Append("@IsDefault,@Type,@Password,@CreatedOn,@Name,@IDNo,@CardNo,@CVV2,@ExpiryDate,@BankCode,@BankName,@BankAbbr,@BillingDate,@RepaymentDate,@BillingEmail,@BankMobile,@MemberAccount,@RepaymentAmount,@RepaymentedAmount,@DepositAmount,@ModifiedOn");
            strSql.Append(")");

            SqlParameter[] parameters = {

                    new SqlParameter("@Name", model.Name),
                    new SqlParameter("@IDNo", model.IDNo),
                    new SqlParameter("@CardNo", model.CardNo),
                    new SqlParameter("@CVV2", model.CVV2),
                    new SqlParameter("@ExpiryDate", model.ExpiryDate),
                    new SqlParameter("@BankCode", model.BankCode),
                    new SqlParameter("@BankName", model.BankName),
                    new SqlParameter("@BankAbbr", model.BankAbbr),
                    new SqlParameter("@BillingDate", model.BillingDate),
                    new SqlParameter("@RepaymentDate", model.RepaymentDate),
                    new SqlParameter("@BillingEmail", model.BillingEmail),
                    new SqlParameter("@BankMobile", model.BankMobile),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@RepaymentAmount", model.RepaymentAmount),
                    new SqlParameter("@RepaymentedAmount", model.RepaymentedAmount),
                    new SqlParameter("@DepositAmount", model.DepositAmount),
                    new SqlParameter("@Type", model.Type),
                    new SqlParameter("@Password", model.Password),
                    new SqlParameter("@IsDefault", model.IsDefault),
                    new SqlParameter("@CreatedOn", DateTime.Now),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CR_BankCard model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [CR_BankCard] set ");
            strSql.Append("[IsDefault]=@IsDefault,[Type]=@Type,[Password]=@Password,[Name]=@Name,[IDNo]=@IDNo,[CardNo]=@CardNo,[CVV2]=@CVV2,[ExpiryDate]=@ExpiryDate,[BankName]=@BankName,[BillingDate]=@BillingDate,[RepaymentDate]=@RepaymentDate,[BillingEmail]=@BillingEmail,[BankMobile]=@BankMobile,[MemberAccount]=@MemberAccount,[RepaymentAmount]=@RepaymentAmount,[RepaymentedAmount]=@RepaymentedAmount,[DepositAmount]=@DepositAmount,[ModifiedOn]=@ModifiedOn");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                    new SqlParameter("@ID", model.ID),
                    new SqlParameter("@Name", model.Name),
                    new SqlParameter("@IDNo", model.IDNo),
                    new SqlParameter("@CardNo", model.CardNo),
                    new SqlParameter("@CVV2", model.CVV2),
                    new SqlParameter("@ExpiryDate", model.ExpiryDate),
                    new SqlParameter("@BankName", model.BankName),
                    new SqlParameter("@BillingDate", model.BillingDate),
                    new SqlParameter("@RepaymentDate", model.RepaymentDate),
                    new SqlParameter("@BillingEmail", model.BillingEmail),
                    new SqlParameter("@BankMobile", model.BankMobile),
                    new SqlParameter("@MemberAccount", model.MemberAccount),
                    new SqlParameter("@RepaymentAmount", model.RepaymentAmount),
                    new SqlParameter("@RepaymentedAmount", model.RepaymentedAmount),
                    new SqlParameter("@DepositAmount", model.DepositAmount),
                    new SqlParameter("@Type", model.Type),
                    new SqlParameter("@IsDefault", model.IsDefault),
                    new SqlParameter("@Password", model.Password),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CR_BankCard] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                new SqlParameter("@ID", ID)

            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_BankCard Get(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [CR_BankCard] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                new SqlParameter("ID", ID)

            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_BankCard Get(DataRow row)
        {
            if (row == null) return null;

            return new EntityHelper<CR_BankCard>().FillEntity(row);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [CR_BankCard] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable GetsByMember(string memberAccount, BankCardType type)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select a.[ID],a.[Name],a.[Type],[IDNo],[CardNo],[BankName],[BillingDate],[RepaymentDate],[BillingEmail],[BankMobile],[MemberAccount],[RepaymentAmount],[RepaymentedAmount],[DepositAmount],[IsDefault],BankCode,BankAbbr from [dbo].[CR_BankCard] a");
            if (memberAccount.Trim() != "")
            {
                strSql.Append(" where MemberAccount=@MemberAccount");
                if (type != BankCardType.All)
                {
                    strSql.Append(" and a.[Type]=@Type");
                }
            }

            return CommonHelper.GetDateTable(DbHelperSQL.Query(strSql.ToString(), new SqlParameter[] { new SqlParameter("@MemberAccount", memberAccount), new SqlParameter("@Type", (int)type) }));
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [CR_BankCard] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CR_BankCard] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CR_BankCard]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by ID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string memberAccount, string bankCode, string bankAccountName, string BankAccountNo)
        {
            string sql = "select count(1) from CR_BankCard where memberAccount = @memberAccount and bankCode = @bankCode and [Name] = @bankAccountName and [CardNo] = @BankAccountNo";

            SqlParameter[] parameters = {
                    new SqlParameter("@memberAccount", memberAccount),
                    new SqlParameter("@bankCode", bankCode),
                    new SqlParameter("@bankAccountName", bankAccountName),
                    new SqlParameter("@BankAccountNo", BankAccountNo)
                                        };

            return DbHelperSQL.Exists(sql, parameters);
        }
        
    }
}


